#include <project.h>
#include "userMain.h"
#include "userUartTxCtrl.h"

#define     UART_TX_DATA_LEN            7
#define     UART_ON
extern unsigned char userKeyValue;
extern unsigned char userRelayStateNow;
extern uint8 userRelayLowPowerFlag;
unsigned char userTxBuf[20];
void userSendKeyInfoFunc(void)          // send keypad info to MASTER
{
    unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x02;
    userTxBuf[4] = userKeyValue;
    if(userRelayLowPowerFlag)
	{
		userTxBuf[5] = userRelayStateNow & 0xF0;
		userTxBuf[5] >>= 4;
	}
	else
	{
		userTxBuf[5] = userRelayStateNow & 0x0F;
	}
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}

#define     NODE_TYPE       0x03
uint8 userLoadNum=0x03;		// num of Relay
//extern uint8 userRelayStateNow;
extern uint8 userLedState;
uint8 userLoadState = 0x33;	// state of Relay
extern uint8 userLightSensorDetectLevel;
extern uint8 userMotionDetect;
extern uint8 userNightState;
extern uint8 userZeroDetect;
uint8 userSensorResult = 0x05;	//
extern uint8 userMotionEnable;
extern uint8 userLightSensorEnable;
extern uint8 userBeepEnable;
extern uint8 userNightEnable;
uint8 userEnableInfo = 0x0b;//0x03;
extern uint8 userBeepVolumeLevel;
extern uint8 userNightLevel;
extern uint8 userLedLevel;
extern uint8 userMotionLevel;
extern uint8 userLightDefaultBindEnable;
extern uint8 userRelayDefaultBindEnable;
uint8 userSensorConfig = 0x3f;//0x07;
extern uint8 userCloseDelay;
extern uint8 userLightSensorLevelDefault;		//the reference light sensor detect Level
uint8 userDefaultConfig= 0x71;//0x73;

void userNodeRegisterInfoUpdateFunc(void)
{
	userLoadNum = NODE_TYPE;
	if(userRelayLowPowerFlag)
	{
		userLoadState = userRelayStateNow & 0xF0;
	}
	else
	{
		userLoadState = userRelayStateNow & 0x0F;
		userLoadState <<= 4;
	}
	userLoadState |= (userLedState & 0xF0) >> 4;
	
	userSensorResult = 0;
	userSensorResult |= userLightSensorDetectLevel << 4;
	if(userMotionDetect) userSensorResult |= 0x01;
	if(userNightState) userSensorResult |= 0x02;
	if(userZeroDetect) userSensorResult |= 0x08;

	userEnableInfo = 0;
	if(userMotionEnable) userEnableInfo |= 0x01;
	if(userLightSensorEnable) userEnableInfo |= 0x02;
	if(userBeepEnable) userEnableInfo |= 0x08;
	//if(userNightEnable) userEnableInfo |= 0x10;

	userSensorConfig = 0;
	if(userBeepVolumeLevel)  userSensorConfig |= 0x01;
	if(userNightLevel)  userSensorConfig |= 0x02;
	if(userLedLevel)  userSensorConfig |= 0x04;
	if(userMotionLevel)  userSensorConfig |= 0x08;
	if(userLightDefaultBindEnable)  userSensorConfig |= 0x10;
	if(userRelayDefaultBindEnable)  userSensorConfig |= 0x20;

	userDefaultConfig = userLightSensorLevelDefault & 0x0F;
	userDefaultConfig <<= 4;
    userDefaultConfig |= userCloseDelay & 0x0F;
}

void userSendNodeRegisterInfoFunc(void)
{
    unsigned char user_i,userTxCheckSum=0;
    userNodeRegisterInfoUpdateFunc();
    userTxBuf[0] = 0x01;        //Identify
    userTxBuf[1] = 13;        //length
    userTxBuf[2] = 0x00;        //MAC ,reserved
    userTxBuf[3] = 0x01;        //Node Type
    userTxBuf[4] = 0x00;        //Version
    userTxBuf[5] = userLoadNum; //num of Load
    userTxBuf[6] = userLoadState;   //Relay State and Distict Led State
    userTxBuf[7] = userSensorResult; //Light Detect Level(4), Motion Detect(1) ,light Led State(1),zero Detect(1)
    userTxBuf[8] = 0x00;            //temperature, reserved
    userTxBuf[9] = userEnableInfo;  //Motion Detect(1),Light Dectect(1),Temperature Detet(1),Beep(1),light Led(1),hand gesture(1)
    userTxBuf[10] = userSensorConfig;//Beep Volume, Light Led Level, Backlight Led Level,Motion Detect Level
                                      //default :light Led if combined with Motion Dectect
                                        //default:the first floor if cmobined with Light Dectect and Motion Detect
    userTxBuf[11] = userDefaultConfig;  //default delay time combined with motion detect, the first Load combined with Light Detect Level
    for(user_i=0;user_i<12;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(13);
}

void userSendRelayInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x01;
    userTxBuf[4] = 0x00;
	if(userRelayLowPowerFlag)
	{
		userTxBuf[5] = userRelayStateNow & 0xF0;
		userTxBuf[5] >>= 4;
	}
	else
	{
		userTxBuf[5] = userRelayStateNow & 0x0F;
	}
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}


void userSendLedInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x04;
	if(userLedState&0x0f)
	{
		userTxBuf[4] = 1;
	}
	else
	{
		userTxBuf[4] = 0;
	}
    userTxBuf[5] = 0x00;
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}


void userSendNightInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x05;
	if(userNightState)
	{
		userTxBuf[4] = 1;
	}
	else
	{
		userTxBuf[4] = 0;
	}
    userTxBuf[5] = 0x00;
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}


void userSendMotionInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x07;
	userTxBuf[4] = 0x00;
	if(userMotionDetect)
	{
		userTxBuf[5] = 1;
	}
	else
	{
		userTxBuf[5] = 0;
	}
    
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}

void userSendLightSensorInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x08;
	userTxBuf[4] = 0x00;
	userTxBuf[5] = userLightSensorDetectLevel;
    
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}

void userSendEnableInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userNodeRegisterInfoUpdateFunc();
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x0C;
	userTxBuf[4] = 0x00;
	userTxBuf[5] = userEnableInfo;
    
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}
void userSendSensorConfigInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userNodeRegisterInfoUpdateFunc();
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0x0F;
	userTxBuf[4] = userSensorConfig;
	userTxBuf[5] = userDefaultConfig;
    
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}

void userSendZeroDetectInfoFunc(void)
{
	unsigned char user_i,userTxCheckSum=0;
    userTxBuf[0] = 0x03;
    userTxBuf[1] = 0x07;
    userTxBuf[2] = 0x00;
    userTxBuf[3] = 0xA0;
	userTxBuf[4] = userZeroDetect;
	userTxBuf[5] = 0x00;
    
    for(user_i=0;user_i<6;user_i++)
    {
        userTxCheckSum += userTxBuf[user_i];
    }
    userTxBuf[user_i] = userTxCheckSum;
    userUartSendData(7);
}

void userAskNetStateFunc(void)
{
    userTxBuf[0] = 0xC0;
    userTxBuf[1] = 0x04;
    userTxBuf[2] = 0x00;
	userTxBuf[3] = 0xC4;
    userUartSendData(0x04);
}

void userUartSendData(unsigned char userTxDataLen)
{
    #ifdef UART_ON
     userTxBufferPushFrame(userTxBuf,userTxDataLen);
    #endif
}

void userUartSendFrameData(unsigned char* src_buf, unsigned char len)
{
    unsigned char user_m; 
    for(user_m=0;user_m<len;user_m++)        
       UART_UartPutChar(*(src_buf+user_m));       // uart
}

unsigned char userTxBuffB[15];
extern unsigned char uUartTxFinishMarkTick;
void userUartTxProcesFunc(void)
{
    #ifdef UART_ON
    static unsigned char uTxStep=0;
    unsigned char uTxLen = 0;
    switch(uTxStep)
    {
        case 0x00:
        {
            userGetTxBufferLen(&uTxLen);
            if(uTxLen==0)  break;
            userTxBufferPop(&userTxBuffB[0]);
            if(userTxBuffB[0]==0x03) 
            {
                userGetTxFrameData(userTxBuffB,7);
                userUartSendFrameData(userTxBuffB,7);
                CyDelay(7);
                uUartTxFinishMarkTick = 0;
                uTxStep = 1;
            }
            if(userTxBuffB[0]==0x01) 
            {
                userGetTxFrameData(userTxBuffB,13);
                userUartSendFrameData(userTxBuffB,13);
                CyDelay(13);
                uUartTxFinishMarkTick = 0;
                uTxStep = 1;
            }
            if(userTxBuffB[0]==0xC0) 
            {
                userGetTxFrameData(userTxBuffB,4);
                userUartSendFrameData(userTxBuffB,4);
                CyDelay(4);
                uUartTxFinishMarkTick = 0;
                uTxStep = 1;
            }
            break;
        }
        case 0x01:
        {
            if(userUartTxIdle()) uTxStep = 0;
            break;
        }
        default:
        {
            break;
        }
    }
    #endif
}
